package com.jsch.test.poxy;

import java.io.*;
import java.net.Socket;

/**
 * 服务器线程，主要来处理多个客户端的请求
 */
public class ServerThead extends ProxyServer implements Runnable {

    Socket socket;
    String socketName;
    private SshdClient sshdClient;

    public ServerThead(Socket socket) {
        this.socket = socket;
        initSshdClient();
    }

    private void initSshdClient() {
        new Thread() {
            @Override
            public void run() {
                sshdClient = new SshdClient();
                try {
                    sshdClient.init();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                sshdClient.setSshdListener(new SshdClient.SshdListener() {
                    @Override
                    public void onSshdResult(byte[] result, int len) {
                        System.out.println("转发给jsch:" + new String(result, 0, len));
                        //转发给jsch
                        try {
                            print(result, len);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }.start();
    }

    @Override
    public void run() {
        try {
            InputStream in = socket.getInputStream();
            //设置该客户端的端点地址
            socketName = socket.getRemoteSocketAddress().toString();
            System.out.println("Client@" + socketName + "已加入聊天");
            //print("Client@" + socketName + "已加入聊天");
            boolean flag = true;
            while (flag) {
                byte[] buf = new byte[10240];
                int len = 0;
                //阻塞，等待该客户端的输出流
                while ((len = in.read(buf)) != -1) {
                    poxyCmd(buf, len);
                    String msg = "Client@" + socketName + ":" + new String(buf, 0, len);
                    System.out.println(msg);
                }
            }

            closeConnect();
        } catch (IOException e) {
            try {
                closeConnect();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     * 代理命令,发送给sshd服务
     *
     * @param line
     */
    private void poxyCmd(byte[] line, int len) {
        sshdClient.sendToSshd(line, len);
    }

    /**
     * 向所有在线客户端socket转发消息
     *
     * @param msg
     * @throws IOException
     */
    private void print(byte[] msg, int len) throws IOException {
        synchronized (socket) {
            OutputStream out = socket.getOutputStream();
            out.write(msg, 0, len);
            out.flush();
        }
    }

    /**
     * 关闭该socket的连接
     *
     * @throws IOException
     */
    public void closeConnect() throws IOException {
        sshdClient.close();
        System.out.println("Client@" + socketName + "已退出聊天");
        //print("Client@" + socketName + "已退出聊天");
        //移除没连接上的客户端
        synchronized (sockets) {
            sockets.remove(socket);
        }
        socket.close();
    }
}